jQuery(document).ready(function() {
	
	initTable();

	bindDict("itemtypecode", "DM_XKXMLX", null);

	//绑定机构下拉列表
	bindOrg2('org');
	
	//列表查询事件
	$('#search').click(function(){
		$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});

	//提交评价
	$('#savecomment').click(function(){
		var iServiceAttitude = $('#serviceattitude').val();
		var iHandlespeed = $('#handlespeed').val();
		
		var msg = '';
		if(iServiceAttitude == ''){
			msg += '请选择<span style="color:red;">服务态度</span>！<br />';
		}
		if(iHandlespeed == ''){
			msg += '请选择<span style="color:red;">办理速度</span>！<br />';
		}
		if(msg != ''){
			$.alert({title:'提醒',  content: msg, confirmButtonClass: 'btn-primary', confirmButton : '确定'});
			return;
		}

		
		var param = $('#comment').serializeObject1();
		
		
		$.ajax({ 
			method: 'post',
			url: projectPath + 'business/savecomment.do',
			data: JSON.stringify(param),
	        dataType: 'json',  
	        contentType:'application/json;charset=UTF-8', 
			success: function(data){
//				alert(JSON.stringify(data));
				$.alert({title:'提醒',  content: data.info, confirmButtonClass: 'btn-primary', confirmButton : '确定',
				    confirm: function(){
			        	if(data.result == 1){
							$('#modalComment').modal('hide');
							$('#search').trigger('click');
						}
			        }
				});
			}
		});
	})	
});		

function initTable(){	
	$('#table').bootstrapTable({
		method: 'post',
	    url: basePath() + 'business/list.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
            	pageNum: params.offset / params.limit + 1,
            	pageSize: params.limit,
            	orgcode: $('#org').attr('data'),
            	itemtypecode: $('#itemtypecode').val(),
            	itemname: $('#itemname').val(),
            	applyno: $('#applyno').val(),
            	startapplytime: $('#startapplytime').val(),
				endapplytime: $('#endapplytime').val()
            };
        },
	    columns: [{
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '50px',
            formatter : function(value, row, index) {  
// 		                return index + 1;  
                var page = $('#table').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }  
        }, {
	        field: 'applyno',
	        title: '申请单号',
	            formatter : function(value, row, index) { 
			        return '<a href="../detail.html?applyno='+ value +'&itemno='+ row.itemno +'" target="_blank">' + value + '</a>';  
			    } 
	    }, {
            align : 'center',
	        field: 'itemname',
	        title: '项目名称'
	    }, {
            align : 'center',
	        field: 'applytime',
	        title: '申请时间',
            formatter : function(value, row, index) {  
		        return formatDate(value, 'yyyy-mm-dd hh:mi');  
		    } 
	    }, {
            align : 'center',
            field: 'orgname',
            title: '受理机构',
	    }, {
            align : 'center',
            field: 'nodename',
            title: '状态',
            formatter : function(value, row, index) {
            	var rValue = value;
            	if(value.indexOf('办结')>=0 || value.indexOf('发证')>=0 || value.indexOf('不') >= 0){
            		if(value.indexOf('不') >= 0)
            			rValue = '<span style="color:red;font-weight: bold;">'+ value +'</span>';
            		else
            			rValue = '<span style="color:green;font-weight: bold;">'+ value +'</span>';
            	}
            	else if(value.indexOf('补正') >= 0)
            		rValue = '<span style="color:red;font-weight: bold;">'+ value +'</span>';
            	else if(value.indexOf('申报') >= 0)
            		rValue = '<span style="color:red; font-weight: bold;">未提交</span>';
            	else if(value.indexOf('受理') >= 0)
            		rValue = '<span style="color:blue; font-weight: bold;">待受理</span>';
            	else
            		rValue = '<span style="color:blue; font-weight: bold;">已受理</span>';

            	return rValue;  
		    } 
	    }]  // 暂时删除评价功能。by yujunxin --2018-12-04
	});
}


function operateFormatter(value, row, index) {
    if(row.nodename == '办结'){
    	if(row.serviceattitude == 0){
    		
		    return [
		        	'<button type="button" class="comment btn btn-link">评价</button>'
		    ].join('');
    	}
    	else{
    		$('#savecomment').hide();
    		
		    return [
		        	'<button type="button" class="comment btn btn-link">查看评价</button>'
		    ].join('');
    	}
    }
}

window.operateEvents = {
  'click .comment': function (e, value, row, index) {		//列表操作栏【评价】
	  $('#applyno1').html(row.applyno);
	  $('#itemname1').html(row.itemname);
	  $('#itemtype').html(row.itemtypename);
	  $('#orgname').html(row.orgname);
	  $('#applyid').val(row.applyid);
	  
	  if(row.serviceattitude == 0){
		  $("#serviceattitude").val(0);
		  $("#handlespeed").val(0);
	  }
	  else{
		  $("#serviceattitude").val(row.serviceattitude);
		  $("#handlespeed").val(row.handlespeed);
	  }
	  $("#serviceattitude").rating('create');
	  $("#handlespeed").rating('create');

      if(row.nodename == '办结'&& row.serviceattitude == 0){
	    	$('#savecomment').show();
	  }
      if(row.nodename == '办结'&& row.serviceattitude > 0){
	    	$('#savecomment').hide();
	  }
	  $('#modalComment').modal('show');
  }
};


function bindOrg1(obj){
	$.ajax({ 
		method: 'post',
		url: projectPath + "share/getOrg.do",
		dataType: "json",
		contentType: "application/json",
		success: function(data){
		    var cityPicker = new IIInsomniaCityPicker({
		        data: data,
		        target: '#' + obj,
		        callback: function(id){
		        	var id = $('#' + obj).attr("data");
		        	var name = $('#' + obj).val();
		        }
		    });

		    cityPicker.init();
		}
	});
}

function bindOrg2(obj){
	$.ajax({ 
		method: 'post',
		url: projectPath + "share/getOrg.do",
		dataType: "json",
		contentType: "application/json",
		success: function(data){
			//历史海事机构
			var historyjson = data.hisResult;
			//海事机构
			var orgAlljson = data.result;
			
			var topCode = '000000';
			var orgList = JSON2ARRAY3(orgAlljson, topCode);
			var orgHistoryList = JSON2ARRAY2(historyjson);
			InitList(obj, topCode, orgList, orgHistoryList);
		}
	});
}
